package com.wx.wxpush.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.wx.wxpush.entiy.Letter;
import com.wx.wxpush.service.LetterService;
import com.wx.wxpush.util.ResultVO;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@Controller
@RequestMapping("/letter")
@Api(tags="匿名信接口")
public class LetterController {
    @Autowired
    private LetterService letterService;


    @GetMapping("/getSecond")
    public ResultVO getSecond(){

        return ResultVO.ok("66666");
    }
    //随机获取匿名信
    @GetMapping("/getLetter")
    public ResultVO getLetter(){
        List<Letter> letterList=letterService.selectRoundBySql();
        String id=null;
        String jso = JSON.toJSONString(letterList);
        JSONArray jsonObject = JSON.parseArray(jso);
        for ( int i = 0; i < jsonObject .size(); i ++) {
            JSONObject ob   = (JSONObject) jsonObject .get( i );
            id= ob.get("id").toString();
        }
        letterService.updateGetter(id);
        return ResultVO.ok(letterList);
    }
    //创建匿名信
    @PostMapping("/createLetter")
    public ResultVO createLetter(@RequestBody Letter letter){
        String letterinfo=letter.getLetterInfo();
        System.out.println(letterinfo.length());

        if(letterinfo.length()<8){
            return ResultVO.ok("没有填写，不提交");

        }
        Boolean creat=letterService.creatLetter( letter.getLetterInfo());

        return ResultVO.ok(creat);
    }
    //喜欢匿名信
    @PostMapping("likeLetter")
    public ResultVO likeLetter(@RequestBody Letter letter){

        int id=letter.getId();
        Boolean b=letterService.likeLetter(id);
        return ResultVO.ok();
    }


    }
